# vPresent is an immersive presentation creation and display application.
# Copyright (C) 2007-2009 by Priority 5 Holdings, Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

from PyQt4 import QtCore, QtGui
import fisheyeui
import gui.util.environment as environ

class Fisheye(QtGui.QWidget, fisheyeui.Ui_FisheyeBase):
   def __init__(self, fisheyeElement, parent=None):
      QtGui.QWidget.__init__(self, parent)
      self.setupUi(self)

      self.mFisheyeElement = fisheyeElement
      self._updateUi()

   def setupUi(self, widget):
      fisheyeui.Ui_FisheyeBase.setupUi(self, widget)

      self.widgets = [self.mRadius, self.mSpan, self.mLatRes, self.mLonRes]

      self.connect(self.mRadius, QtCore.SIGNAL("valueChanged(double)"),
                   self.onValueChanged)
      self.connect(self.mSpan, QtCore.SIGNAL("valueChanged(double)"),
                   self.onValueChanged)
      self.connect(self.mLatRes, QtCore.SIGNAL("valueChanged(int)"),
                   self.onValueChanged)
      self.connect(self.mLonRes, QtCore.SIGNAL("valueChanged(int)"),
                   self.onValueChanged)

   def _updateUi(self):
      # Disable signals while changing values.
      for w in self.widgets:
         w.blockSignals(True)

      self.mRadius.setValue(self.mFisheyeElement.dr3d_size)
      self.mSpan.setValue(self.mFisheyeElement.vpresent_span)
      self.mLatRes.setValue(self.mFisheyeElement.vpresent_latres)
      self.mLonRes.setValue(self.mFisheyeElement.vpresent_lonres)

      for w in self.widgets:
         w.blockSignals(False)

   def _updateElement(self):
      self.mFisheyeElement.dr3d_size = self.mRadius.value()
      self.mFisheyeElement.vpresent_span = self.mSpan.value()
      self.mFisheyeElement.vpresent_latres = self.mLatRes.value()
      self.mFisheyeElement.vpresent_lonres = self.mLonRes.value()
      self.mFisheyeElement._update(True)
      env = environ.Environment()
      env.updateGL()

   def onValueChanged(self, value):
      self._updateElement()
